<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-copy"></i> 订单管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量停用</el-button>
                <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                    <el-option key="1" label="广东省" value="广东省"></el-option>
                    <el-option key="2" label="湖南省" value="湖南省"></el-option>
                </el-select> -->
                <el-input v-model="query.name" placeholder="输入内容来进行模糊查找" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="订单标题"></el-table-column>
                <el-table-column prop="type" label="订单类型"></el-table-column>
                <el-table-column prop="startTime" label="开始时间"></el-table-column>
                <el-table-column prop="endTime" label="结束时间"></el-table-column>
                <el-table-column prop="location" label="位置"></el-table-column>
                <el-table-column prop="principalAmount" label="本金金额"></el-table-column>
                <el-table-column prop="depositAmount" label="押金金额"></el-table-column>
                <el-table-column prop="bountyAmount" label="赏金金额"></el-table-column>
                <el-table-column prop="commisionAmount" label="佣金金额"></el-table-column>
                <el-table-column prop="reportSymbol" label="举报情况"></el-table-column>
                <el-table-column prop="releasedTime" label="发帖时间"></el-table-column>

                <el-table-column prop="receiveTime" label="施助者接受时间"></el-table-column>
                <el-table-column prop="helperId" label="施助者ID"></el-table-column>
            </el-table>

            <!-- <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div> -->
        </div>

        <!-- 编辑弹出框 -->
        <!-- <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog> -->
    </div>
</template>

<script>
// import { fetchData } from '../../api/index';
import axios from '../../axios/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            axios.get('/manager/help/get', {})
            .then(response => {
                const res = response.data;
                
                // 過濾掉 isDraft 為 1 的數據
                this.tableData = res.filter(item => item.isDraft !== 1).map(item => {
                    // 替換時間字段中的空格為換行符
                    item.startTime = item.startTime ? item.startTime.replace(" ", "\n") : '';
                    item.endTime = item.endTime ? item.endTime.replace(" ", "\n") : '';
                    item.releasedTime = item.releasedTime ? item.releasedTime.replace(" ", "\n") : '';
                    item.receiveTime = item.receiveTime ? item.receiveTime.replace(" ", "\n") : '';
                    item.commisionAmount = item.commisionAmount.toFixed(2);
                    // 舉報情況字段處理
                    item.reportSymbol = item.reportSymbol === 0 ? '沒有举报' : '收到举报';

                    return item;
                });
                console.log(this.tableData);
            })
            .catch(error => {
                console.error("获取数据失败: ", error);
            });
        },

        // 触发搜索按钮    
        handleSearch() {
            const searchText = this.query.name || '';  // 用戶輸入的查找內容
            
            // 調用 axios 獲取數據
            axios.get('/manager/help/get', {})
                .then(response => {
                    const res = response.data;
                    
                    // 過濾掉 isDraft 為 1 的數據
                    let filteredData = res.filter(item => item.isDraft !== 1).map(item => {
                        // 替換時間字段中的空格為換行符
                        item.startTime = item.startTime ? item.startTime.replace(" ", "\n") : '';
                        item.endTime = item.endTime ? item.endTime.replace(" ", "\n") : '';
                        item.releasedTime = item.releasedTime ? item.releasedTime.replace(" ", "\n") : '';
                        item.receiveTime = item.receiveTime ? item.receiveTime.replace(" ", "\n") : '';
                        item.commisionAmount = item.commisionAmount.toFixed(2);
                        // 舉報情況字段處理
                        item.reportSymbol = item.reportSymbol === 0 ? '沒有举报' : '收到举报';

                        return item;
                    });

                    // 如果有搜索條件，進行模糊搜索篩選
                    if (searchText.trim() !== '') {
                        filteredData = filteredData.filter(item => {
                            // 遍歷每個字段，檢查是否有字段符合搜索條件
                            return Object.keys(item).some(key => {
                                const value = item[key] !== null ? item[key].toString() : '';  // 將所有字段轉換為字符串進行比較
                                return value.includes(searchText);
                            });
                        });
                    }

                    // 最終更新表格數據
                    this.tableData = filteredData;
                })
                .catch(error => {
                    console.error("模糊查找失敗: ", error);
                });
        },


        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        // saveEdit() {
        //     this.editVisible = false;
        //     const params = {
        //         id: this.idx,
        //         reply: this.form.reply
        //     };
        //     axios.post('/manager/notice/reply', params)
        //         .then(response => {
        //         this.$message.success(`回覆成功`);
        //         })
        //         .catch(error => {
        //         console.error("回覆失敗: ", error);
        //         });
        // }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

</style>
